﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASMS.Excel2DbTool.Utils
{
    /// <summary>
    /// 空运航班热力V2
    /// </summary>
    public class 空运航班热力V2
    {
        public static void Run(string filePath, string destFileName)
        {
            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            StringBuilder nameBuilder = new StringBuilder();
            StringBuilder valueBuilder = new StringBuilder();
            List<HeatmapItem> names = new List<HeatmapItem>();
            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string geoString = Convert.ToString(dataTable.Rows[index][12]).Trim();
                string valueString = Convert.ToString(dataTable.Rows[index][9]).Trim();

                if (string.IsNullOrEmpty(geoString)
                    || string.IsNullOrEmpty(valueString))
                    continue;

                double value = 0;
                if (!double.TryParse(valueString, out value))
                {
                    continue;
                }

                string[] geoArr = geoString.Split(new char[] { ',' });
                if (geoArr.Length != 2)
                {
                    continue;
                }
                double longitude = 0;
                double latitude = 0;
                if (double.TryParse(geoArr[0], out longitude) == false
                    || double.TryParse(geoArr[1], out latitude) == false)
                {
                    continue;
                }

                bool isInsert = false;
                for (int nameIndex = 0; nameIndex < names.Count; nameIndex++)
                {
                    if (names[nameIndex].X == longitude && names[nameIndex].Y == latitude)
                    {
                        names[nameIndex].Value += value;
                        isInsert = true;
                    }
                }

                if (!isInsert)
                {
                    names.Add(new HeatmapItem(longitude, latitude, value));
                }
            }

            for (var index = 0; index < names.Count; index++)
            {
                if (index == 0)
                {
                    nameBuilder.AppendFormat("[{0},{1},{2}]", names[index].X, names[index].Y, names[index].Value);
                }
                else
                {
                    nameBuilder.AppendFormat(",[{0},{1},{2}]", names[index].X, names[index].Y, names[index].Value);
                }
            }
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("{{\"heatmap\":[{0}]}}", nameBuilder, valueBuilder);

            string destFilePath = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, destFileName);
            System.IO.File.WriteAllText(destFilePath, builder.ToString());
            Console.WriteLine(string.Format("转换完毕{0},count:{1}", destFilePath, dataTable.Rows.Count));
        }
    }
}